<?php

class ResetPasswordValidator extends sfValidatorSchema
{
  public function __construct($options = array(), $messages = array())
  {
    parent::__construct(null, $options, $messages);
  }
  
  protected function configure($options = array(), $messages = array())
  {
    $this->addRequiredOption('model');
    $this->addRequiredOption('email');
    $this->addRequiredOption('password_token');
    
    $this->addMessage('invalid_email'         , 'El email ingresado no es valido.');
    $this->addMessage('invalid_password_token', 'El link que tu as seguido es invalido o incompleto. Inteneta copiando y pewgando el link completo a tu barra de direcciones.');
  }
  
  protected function doClean($values)
  {
    if (!is_array($values))
    {
      throw new InvalidArgumentException('You must pass an array parameter to the clean() method (this validator can only be used as a post validator).');
    }
    
    // not validate when any field is blank
    foreach ($values as $value)
    {
      if (empty($value)) return $values;
    }
    
    $user = Doctrine::getTable($this->getOption('model'))->findOneByEmail($this->getOption('email'));
    if ($values['email'] != $this->getOption('email'))
    {
      $error = 'invalid_email';
    }
    elseif (!$user->isValidPasswordToken($this->getOption('password_token')))
    {
      $error = 'invalid_password_token';
    }
    else
    {
      return $values;
    }
    
    $error = new sfValidatorError($this, $error);
    
    throw $error;
  }
}
